package com.cx.android;

import android.content.Context;
import android.content.Intent;

import android.app.PendingIntent;
import android.widget.RemoteViews;

public class Code1009Intents
{
	public static final String ACTION_REFRESH_WIDGETS = "ACTION_REFRESH_WIDGETS";
	public static final String ACTION_SHOW_CODE1009   = "ACTION_SHOW_CODE1009";
	public static final int    EXTRA_INT_INVALIDVALUE = -1;
	
	public static Intent refreshWidgets(Context context)
	{
		final Intent intent = new Intent(context, Code1009IntentService.class);
		
		intent.setAction(ACTION_REFRESH_WIDGETS);
		
		return intent;
	}

    public static Intent showCode1009(Context context) 
    {                
    	final Intent intent = new Intent(context, Code1009Activity.class);  
		
    	intent.setAction(ACTION_SHOW_CODE1009);                 

    	return intent;         
   	} 	
	
	/////////////////////////////////////////////////////////////////////////
	//=======================================================================
	private static final int PENDING_INTENT_FLAGS = PendingIntent.FLAG_UPDATE_CURRENT;

	private static int generateRequestCode(int resId, int widgetId) 
	{
		StringBuilder sb = new StringBuilder();
		sb.append(resId);
		sb.append(" ");
		sb.append(widgetId);
		
		// Only needed if we have more problems with uniqueness.
		//sb.append(System.currentTimeMillis());
		return sb.toString().hashCode();
	}

	public static final void BindClickAction(
		final Context     context  , 
		final RemoteViews views    ,
		final int         resId    ,
		final int         widgetId , 
		final Intent      intent   ) 
	{
		final int     requestCode = generateRequestCode(resId, widgetId);		
		PendingIntent pIntent     = PendingIntent.getService(context, requestCode, intent, PENDING_INTENT_FLAGS);
		
		views.setOnClickPendingIntent(resId, pIntent);
	}

	public static final void BindClickActionActivity(
		final Context     context  , 
		final RemoteViews views    , 
		final int         resId    ,
		final int         widgetId , 
		final Intent      intent   ) 
	{
		final int     requestCode = generateRequestCode(resId, widgetId);
		PendingIntent pIntent     = PendingIntent.getActivity(context, requestCode, intent, PENDING_INTENT_FLAGS);
		
		views.setOnClickPendingIntent(resId, pIntent);
	}

}
